﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

using AimGameEngine.EditorObject;

using AimGameLibrary.GameObject;

namespace AimGameEngine.NodeEditorObject
{
    partial class PropretyAnimation2D : Form
    {
        NodeAnimation2D nodeanimation2d;
        public PropretyAnimation2D(NodeAnimation2D nodeanimation2d)
        {
            InitializeComponent();

            this.nodeanimation2d = nodeanimation2d;
            
            //keyframedata = inputkeyframedata;
            foreach (String Actionadd in this.nodeanimation2d.MyKeyFrameData.MyActionName)
                Action_List.Items.Add(Actionadd);

            //framedata = inputframedata;

            this.Width_Pixel.Text = this.nodeanimation2d.MyFrameData.Width.ToString();
            this.Height_Pixel.Text = this.nodeanimation2d.MyFrameData.Height.ToString();
            this.NumActionColum.Value = this.nodeanimation2d.MyFrameData.CountColum;
            this.NumActionRow.Value = this.nodeanimation2d.MyFrameData.CountRow;
            this.NumFrameRate.Value = this.nodeanimation2d.MyFrameData.FrameRate;

            this.numValue.Value = this.nodeanimation2d.myint;
            this.InputMode.SelectedItem = this.nodeanimation2d.InputMode.ToString();
        }

        private void AddAction_Click(object sender, EventArgs e)
        {
            if (this.ItemName.Text != "")
            {
                if (this.Action_List.Items.Count == 0)
                {
                    this.Action_List.Items.Add(ItemName.Text);
                    this.nodeanimation2d.MyKeyFrameData.MyActionName.Add(ItemName.Text);
                    this.nodeanimation2d.MyKeyFrameData.MyKeyFrame.Add(new List<int>());
                    this.nodeanimation2d.MyKeyFrameData.MyPlayLoop.Add(false);
                    ItemName.Text = "";
                }
                else {
                    bool CanAddthisName = true;
                    foreach (String ChackName in Action_List.Items)
                    {
                        if (ItemName.Text == ChackName)
                        {
                            CanAddthisName = false;
                            MessageBox.Show("Please replace Name");
                            break;
                        }
                    }

                    if (CanAddthisName)
                    {
                        this.Action_List.Items.Add(ItemName.Text);
                        this.nodeanimation2d.MyKeyFrameData.MyActionName.Add(ItemName.Text);
                        this.nodeanimation2d.MyKeyFrameData.MyKeyFrame.Add(new List<int>());
                        this.nodeanimation2d.MyKeyFrameData.MyPlayLoop.Add(false);
                        ItemName.Text = "";
                    }
                
                }
            }
            else
            {
                MessageBox.Show("Don't Set Name");
            }
        }

        private void DeleteAction_Click(object sender, EventArgs e)
        {
            if (this.Action_List.SelectedItem != null)
            {
                int ActionSelecte = this.Action_List.Items.IndexOf(this.Action_List.SelectedItem);

                this.nodeanimation2d.MyKeyFrameData.MyKeyFrame.RemoveAt(ActionSelecte);
                this.nodeanimation2d.MyKeyFrameData.MyActionName.RemoveAt(ActionSelecte);
                this.Action_List.Items.RemoveAt(ActionSelecte);
                
                // Clean Show Data
                KeyFrame_List.Items.Clear();
            }
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            this.KeyFrame_List.Items.Clear();
            this.NumtoSetKeyFrame.Value = 0;
            if (Action_List.SelectedItem != null)
            {
                //MessageBox.Show(GetItem);
                
                int ActionSelecte = this.Action_List.Items.IndexOf(Action_List.SelectedItem);
                List<int> GetAction = this.nodeanimation2d.MyKeyFrameData.MyKeyFrame[ActionSelecte];

                foreach (int Keyframe in GetAction)
                {
                    this.KeyFrame_List.Items.Add(Keyframe);
                }
                ActionLoop.Checked = this.nodeanimation2d.MyKeyFrameData.MyPlayLoop[ActionSelecte];
            }


        }

        private void AddKeyFrame_Click(object sender, EventArgs e)
        {
            if (this.Action_List.SelectedItem != null)
            {
                //int count = (int);
                int ActionSelecte = this.Action_List.Items.IndexOf(this.Action_List.SelectedItem);
                this.KeyFrame_List.Items.Add(this.NumtoSetKeyFrame.Value);
                this.nodeanimation2d.MyKeyFrameData.MyKeyFrame[ActionSelecte].Add((int)this.NumtoSetKeyFrame.Value);
                if(this.AutoKey.Checked)
                this.NumtoSetKeyFrame.Value++;
            }
        }

        private void DeleteKeyFrame_Click(object sender, EventArgs e)
        {
            if (this.KeyFrame_List.SelectedItem != null)
            {
                int KeyFrameSelecte = this.KeyFrame_List.Items.IndexOf(this.KeyFrame_List.SelectedItem);
                int ActionSelecte = this.Action_List.Items.IndexOf(this.Action_List.SelectedItem);

                this.nodeanimation2d.MyKeyFrameData.MyKeyFrame[ActionSelecte].RemoveAt(KeyFrameSelecte);

                KeyFrame_List.Items.RemoveAt(KeyFrameSelecte);
            }
        }

        private void ActionLoop_CheckedChanged(object sender, EventArgs e)
        {
            if(Action_List.SelectedItem != null)
            {
                int ActionSelecte = this.Action_List.Items.IndexOf(this.Action_List.SelectedItem);
                this.nodeanimation2d.MyKeyFrameData.MyPlayLoop[ActionSelecte] = this.ActionLoop.Checked; 
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.nodeanimation2d.MyFrameData.CountColum = (int)this.NumActionColum.Value;
            this.nodeanimation2d.MyFrameData.CountRow = (int)this.NumActionRow.Value;
            this.nodeanimation2d.MyFrameData.FrameRate = (int)this.NumFrameRate.Value;

            String tempString = (String)this.InputMode.SelectedItem;

            String tempOldmode = nodeanimation2d.InputMode.ToString();
            if (tempString == AimGameLibrary.NodeInputMode.Int_1.ToString())
                this.nodeanimation2d.InputMode = AimGameLibrary.NodeInputMode.Int_1;
            else
            {
                this.nodeanimation2d.myint = (int)this.numValue.Value;
                this.nodeanimation2d.InputMode = AimGameLibrary.NodeInputMode.None;
            }

            if(tempOldmode != tempString)
            nodeanimation2d.SetupProprety();
                
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String tempString = (String)this.InputMode.SelectedItem;

            if (tempString == AimGameLibrary.NodeInputMode.Int_1.ToString())
                this.numValue.Enabled = false;
            else this.numValue.Enabled = true;
        }

    }
}
